/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxitemdapi.p                                     */
/* PROCEDURE TYPE        : JSON API Process                                 */
/* DESCRIPTION           : Item Master Load CIM-LOAD Process                */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                6-Mar-24   */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}


{xxapilog.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vi_reccount           as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


define temp-table ttptidet no-undo
    
            field oid_pti_det              like pti_det.oid_pti_det     
            field pti_part                 like pti_det.pti_part        
            field pti_site                 like pti_det.pti_site        
            field pti_abc                  like pti_det.pti_abc         
            field pti_lot_ser              like pti_det.pti_lot_ser     
            field pti_loc                  like pti_det.pti_loc         
            field pti_loc_type             like pti_det.pti_loc_type    
            field pti_auto_lot             like pti_det.pti_auto_lot    
            field pti_avg_int              like pti_det.pti_avg_int     
            field pti_cyc_int              like pti_det.pti_cyc_int     
            field pti_shelflife            like pti_det.pti_shelflife   
            field pti_critical             like pti_det.pti_critical    
            field pti_rctpo_status         like pti_det.pti_rctpo_status
            field pti_rctpo_active         like pti_det.pti_rctpo_active
            field pti_rctwo_status         like pti_det.pti_rctwo_status
            field pti_rctwo_active         like pti_det.pti_rctwo_active
            field pti_memo_type            like pti_det.pti_memo_type 
            field pti_traceid              as   character
            field pti_dataid               as   character 
            field pti_cimfile               as   character 
            field pti_logfile               as   character 
            
            
            . 

      DEFINE DATASET dsPartsitelocdetail FOR ttptidet.

      hdataset = dataset dsPartsitelocdetail:HANDLE.    


mainloop:
repeat on error undo mainloop, retry mainloop:

   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "pt/"
       m_archpath    = m_archpath + "pt/"
       m_successpath = m_successpath + "pt/"
       m_errorpath   = m_errorpath + "pt/"
       m_workpath    = m_workpath + "pt/".


   empty temp-table ttptidet.
   
   
   v_runproagram = "".
     vc_traceid = "".
   /* Load item master data from JSON  */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttptidet no-error.
      assign vc_traceid = ttptidet.pti_traceid.
   end.
  /* Create CIM-LOAD file and do cim-load */
  v_cim = string(vc_traceid).
  v_log = string(vc_traceid).
  
 output to /apps/eai/work/cim/xxitemsiteloc.log  .      
 
if vc_import and vl_RetOK then do:  

  /* create cim load format */
                
  cimloadprocess:
  do transaction on error undo cimloadprocess, leave cimloadprocess: 
  
  for each ttptidet  break by ttptidet.pti_part.

   if first-of(ttptidet.pti_part) then do:

       v_cim = m_workpath + "PTI" + "_" + string(ttptidet.pti_dataid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
	   v_log = m_workpath + "PTI" + "_" + string(ttptidet.pti_dataid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
	 
     output stream f_cim to value(v_cim). 
   end.
	 put stream f_cim  unformatted 
	 
	         v_quote ttptidet.pti_part v_quote v_space                        /* Part number */
			 v_quote ttptidet.pti_site v_quote skip                           /* Site        */
			 v_quote ttptidet.pti_abc     v_quote                             /* ABC class　*/ 
			 v_default v_space                                                /* Lot control　*/ 
			 v_quote ttptidet.pti_loc     v_quote                             /* Location　*/
			 v_default v_space                                                /* LocationType */
			 v_default v_space                                                /* Auto lot */
			 v_quote ttptidet.pti_avg_int     v_quote  v_space                /* AVERAGE interval */
			 v_quote ttptidet.pti_cyc_int     v_quote  v_space                /* CycleCountInterval */
			 v_quote ttptidet.pti_shelflife   v_quote  v_space                /* shelf life */
			 v_quote ttptidet.pti_critical    v_quote  v_space                /* Critical Part */
			 v_default v_space                                                /* PO status */
	         v_default v_space                                                /* PO status active */
			 v_default v_space                                                /* Wo status */
			 v_default v_space                                                /* WO status active */
			 v_default v_space      skip.                                     /* Non inventory order */
	 
	 output stream f_cim close.
	 
   
   if last-of(ttptidet.pti_part) then do:
    output stream f_cim close.
    
         
     /* cim load process */
     v_runproagram = "pppsmt01.p".
     execname = "xxitemsitelocapi.p".

     
       put unformatted v_cim " " skip
                       v_log " " skip 
                       m_outpath " " skip
                       m_archpath " " skip
                       v_runproagram " " skip
                       v_errstring " " skip 
                       v_errorstatus skip .
                       
       run cimloadprocess(input v_cim,
                          input v_log,
                          input v_runproagram,
                          input vc_traceid,
                          input ttptidet.pti_dataid,
                          input ttptidet.pti_part,
                          output v_errstring,
                          output v_errorstatus) .
                          
       v_toterrorstatus = v_toterrorstatus or v_errorstatus.    


            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).   



                                     
                 
       end. /* do vi_filecount = 1 to 2: */   
       
       
       
         assign   ttptidet.pti_cimfile = replace(v_cim,m_workpath,vc_topath)
                    ttptidet.pti_logfile = replace(v_log,m_workpath,vc_topath).
       
       
       
       
       
       
     end. /* if last-of */
     
    end. /* for each ttrodet  */
     if  v_toterrorstatus then 
                 undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */
       if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
       end.  /* if can-find(first api_status */
         


   end. /* if vc_import */
   
        /* Handle cimload related files */
        
       for each ttptidet break by ttptidet.pti_part .
       if last-of(ttptidet.pti_part) then 
       do:
       vi_reccount = vi_reccount + 1.
              
       run CreateAPILogMaster(input vc_traceid,
                              input "itemsiteLocdetail",
                              input "xxitemsitelocapi.p",
                              input ttptidet.pti_cimfile,
                              input ttptidet.pti_logfile,
                              input vc_inputjson,
                              input ttptidet.pti_part,
                              input "",
                              input "xxapiitemlocdata_det",
                              input vi_reccount).  
                              
       end. /*  if last-of(ttptidet.pti_part) then  */
        
      end. /*   for each ttptidet break by ttptidet.pti_part . */
        
        
        
        
        
        
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
       
       output close.   
   
   
   leave.
end. /* repeat on error undo mainloop, retry mainloop: */
